<?php

namespace app\admin\model;
use think\facade\Config;
use think\Model;

class GoodsProp extends Model{
    public function getShanchuAttr($value){
        if (Config::has('param.param_shanchu')){
            $config = Config::get('param.param_shanchu');
            return $config[$value];
        }
    }
    public function searchKeywordsAttr($query,$value,$data){
        if(''!==$value){
            $query->whereLike('pname','%'.$value.'%');
        }

    }
    public function searchFuAttr($query,$value){
        if(!empty($value)){
            foreach ($value as $k=>$v){
                $query->whereFindInSet('pvalues',$v);
            }

        }
    }
    public static function prop($data=[],$query=[],$page=true){
        $model = self::field(true)->withSearch(array_keys($data),$data);
//        if(isset($data['keywords']) && '' !== $data['keywords']){
//            $model->whereLike('pname',"%{$data['keywords']}%");
//        }
//        if(!empty($data['fu'])){
//            foreach ($data['fu'] as $k=>$v){
//                $model->whereFindInSet('pvalues',$v);
//            }
//
//        }
        if($page){
            return $model->paginate([
            'list_rows'=>2,
            'query'=>$query
        ])->each(function ($v,$k){
            $shu=self::table('ecm_goods_prop_value')
                    ->field('pid,prop_value,vid')
                   ->where("vid in ({$v['pvalues']})")
                  ->select();
                $arr=[];
            foreach ($shu as $kk=>$vv){
                $arr[]=$vv['prop_value'];
                $a=implode(',',$arr);
            }

            $v['b']=$a;

            return $v;
        });
        }
        return $model->select();
    }
}